/*
 ********************************************************************************
 * @FileName  : usb_port.h
 * @CreateDate: 2021-5-17
 * @Author    : GaoQiu
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 ********************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */

#ifndef USB_PORT_H_
#define USB_PORT_H_

#include "platform/platform.h"

/*! USB register define. */
typedef struct{
	__IO uint8_t  EDP0PTR;      /*!< 0x00  control endpoint data pointer. */
	__IO uint8_t  EDP0DAT;      /*!< 0x01  control endpoint data fifo. */
	__IO uint8_t  EDP0CTRL;     /*!< 0x02  control endpoint control. */
	__IO uint8_t  EDP0STA;      /*!< 0x03  control endpoint state.  */
	__IO uint8_t  EDP0MODE;     /*!< 0x04  control endpoint mode. */

	__IO uint8_t  USBCTRL;      /*!< 0x05  */
	__I  uint16_t CLK_CALC;     /*!< 0x06  */
	__IO uint16_t rsvd1;        /*!< 0x08  */
	__IO uint8_t  DEV_MODE;     /*!< 0x0a  device power support mode. */
	__IO uint8_t  EDP0SIE;      /*!< 0x0b  */
	__IO uint8_t  SUSPEND_CYC;  /*!< 0x0c  */
	__I  uint8_t  INTFACE_ALT;  /*!< 0x0d interface alternate. */

	__IO uint16_t rvsd2;        /*!< 0x0e  */

	__IO uint8_t  EDPS8PTR;     /*!< 0x10  endpoint8 data fifo pointer. */
	__IO uint8_t  EDPS1PTR;     /*!< 0x11  endpoint1 data fifo pointer. */
	__IO uint8_t  EDPS2PTR;     /*!< 0x12  */
	__IO uint8_t  EDPS3PTR;     /*!< 0x13  */
	__IO uint8_t  EDPS4PTR;     /*!< 0x14  */
	__IO uint8_t  EDPS5PTR;     /*!< 0x15  */
	__IO uint8_t  EDPS6PTR;     /*!< 0x16  */
	__IO uint8_t  EDPS7PTR;     /*!< 0x17  */

	__IO uint8_t  EDPS8DATA;    /*!< 0x18  endpoint8 data fifo. */
	__IO uint8_t  EDPS1DATA;    /*!< 0x19  endpoint1 data fifo.*/
	__IO uint8_t  EDPS2DATA;    /*!< 0x1a  */
	__IO uint8_t  EDPS3DATA;    /*!< 0x1b  */
	__IO uint8_t  EDPS4DATA;    /*!< 0x1c  */
	__IO uint8_t  EDPS5DATA;    /*!< 0x1d  */
	__IO uint8_t  EDPS6DATA;    /*!< 0x1e  */
	__IO uint8_t  EDPS7DATA;    /*!< 0x1F  */

	__IO uint8_t  EDPS8CTRL;    /*!< 0x20  endpoint8 control */
	__IO uint8_t  EDPS1CTRL;    /*!< 0x21  endpoint1 control */
	__IO uint8_t  EDPS2CTRL;    /*!< 0x22  */
	__IO uint8_t  EDPS3CTRL;    /*!< 0x23  */
	__IO uint8_t  EDPS4CTRL;    /*!< 0x24  */
	__IO uint8_t  EDPS5CTRL;    /*!< 0x25  */
	__IO uint8_t  EDPS6CTRL;    /*!< 0x26  */
	__IO uint8_t  EDPS7CTRL;    /*!< 0x27  */

	__IO uint8_t  EDPS8ADDR;    /*!< 0x28  */
	__IO uint8_t  EDPS1ADDR;    /*!< 0x29  */
	__IO uint8_t  EDPS2ADDR;    /*!< 0x2a  */
	__IO uint8_t  EDPS3ADDR;    /*!< 0x2b  */
	__IO uint8_t  EDPS4ADDR;    /*!< 0x2c  */
	__IO uint8_t  EDPS5ADDR;    /*!< 0x2d  */
	__IO uint8_t  EDPS6ADDR;    /*!< 0x2e  */
	__IO uint8_t  EDPS7ADDR;    /*!< 0x2f  */

	__IO uint8_t  USBRAM;       /*!< 0x30  */

	__IO uint8_t  rsvd3;        /*!< 0x31  */
	__IO uint8_t  rsvd4;        /*!< 0x32  */
	__IO uint8_t  rsvd5;        /*!< 0x33  */
	__IO uint32_t rsvd6;        /*!< 0x34 - 0x37 */

    __IO uint8_t  USBISO;       /*!< 0x38  */

    __IO uint8_t  USB_IRQ_STA;   /*!< 0x39 Write 1 clear 0. */
    __IO uint8_t  USB_MASK;      /*!< 0x3a  */

    __IO uint8_t  USB_MAX0;     /*!< 0x3b  */
    __IO uint8_t  USB_MIN0;     /*!< 0x3c  */
    __IO uint8_t  USB_FIFO;     /*!< 0x3d  */

    __IO uint8_t  USB_MAX;      /*!< 0x3e unit: 8bytes*/

    __IO uint8_t  USB_TICK;     /*!< 0x3f */
}USB_Type_t;

/*! USB */
#define USB     ((USB_Type_t*)(USB_BASE))


#define USB_EDP_NUM    9 /*!< control endpoint + data endpoint. */

/*! USB endpoint id define */
#define USB_EDP_8      0
#define USB_EDP_1      1
#define USB_EDP_2      2
#define USB_EDP_3      3
#define USB_EDP_4      4
#define USB_EDP_5      5
#define USB_EDP_6      6
#define USB_EDP_7      7



/*! USB endpoint0 IT State Flag define. */
#define USB_EDP0_IT_Flag_Trans         (uint8_t)0x0F
#define USB_EDP0_IT_Flag_Setup         (uint8_t)0x10 /*!< Setup Stage. */
#define USB_EDP0_IT_Flag_Data          (uint8_t)0x20 /*!< Setup data Stage. */
#define USB_EDP0_IT_Flag_Status        (uint8_t)0x40 /*!< Setup status Stage. */
#define USB_EDP0_IT_Flag_Interface     (uint8_t)0x80

/*! USB endpoint0 Control Flag define. */
#define USB_EDP0_Ctrl_Flag_Data_Ack        (uint8_t)0x01
#define USB_EDP0_Ctrl_Flag_Data_Stall      (uint8_t)0x02
#define USB_EDP0_Ctrl_Flag_State_Ack       (uint8_t)0x04
#define USB_EDP0_Ctrl_Flag_State_Stall     (uint8_t)0x08

/*! USB endpoint0 mode define. */
#define USB_EDP0_Mode_Auto_Addr    (uint8_t)0x01
#define USB_EDP0_Mode_Auto_Cfg     (uint8_t)0x02
#define USB_EDP0_Mode_Auto_Intf    (uint8_t)0x04
#define USB_EDP0_Mode_Auto_Sta     (uint8_t)0x08
#define USB_EDP0_Mode_Auto_Sync    (uint8_t)0x10
#define USB_EDP0_Mode_Auto_Desc    (uint8_t)0x20
#define USB_EDP0_Mode_Auto_Feat    (uint8_t)0x40
#define USB_EDP0_Mode_Auto_Std     (uint8_t)0x80



/*! USB endpoint IT define */
#define USB_EDP8_IT       (uint8_t)0x01
#define USB_EDP1_IT       (uint8_t)0x02
#define USB_EDP2_IT       (uint8_t)0x04
#define USB_EDP3_IT       (uint8_t)0x08
#define USB_EDP4_IT       (uint8_t)0x10
#define USB_EDP5_IT       (uint8_t)0x20
#define USB_EDP6_IT       (uint8_t)0x40
#define USB_EDP7_IT       (uint8_t)0x80


/*! USB endpoint IT flag define */
#define USB_EDP8_IT_Flag  (uint8_t)0x01
#define USB_EDP1_IT_Flag  (uint8_t)0x02
#define USB_EDP2_IT_Flag  (uint8_t)0x04
#define USB_EDP3_IT_Flag  (uint8_t)0x08
#define USB_EDP4_IT_Flag  (uint8_t)0x10
#define USB_EDP5_IT_Flag  (uint8_t)0x20
#define USB_EDP6_IT_Flag  (uint8_t)0x40
#define USB_EDP7_IT_Flag  (uint8_t)0x80


/*! USB endpoint control flag. */
#define USB_EDP_Ctrl_Flag_Busy    (uint8_t)0x01
#define USB_EDP_Ctrl_Flag_Stall   (uint8_t)0x02
#define USB_EDP_Ctrl_Flag_Data0   (uint8_t)0x04
#define USB_EDP_Ctrl_Flag_Data1   (uint8_t)0x08
#define USB_EDP_Ctrl_Flag_Mono    (uint8_t)0x40
#define USB_EDP_Ctrl_Flag_Eof_Iso (uint8_t)0x80

/*** USB Endpoint0 API ***/
uint8_t USB_GetEdp0ITStateFlag(void);
void    USB_ClearEdp0ITStateFlag(uint8_t EDP_IT_Flag);
void    USB_SetEdp0CtrlFlag(uint8_t EDP_Ctrl_Flag);
uint8_t USB_GetEdp0Counter(void);
void    USB_ClearEdp0Counter(void);
uint8_t USB_ReadEdp0Data(void);
void    USB_WriteEdp0(uint8_t *pData, uint32_t len);

/*** USB Endpoint(1-8) API ***/
uint8_t USB_GetEdpITStatusFlag(uint8_t EDP_IT_Flag);
void    USB_ClearEdpITStatusFlag(uint8_t EDP_IT_Flag);
void    USB_ClearAllITStatus(void);
void    USB_ITConfig(uint8_t EDP_IT, bool_t enable);

uint8_t USB_GetEdpCounter(uint8_t edp);
void    USB_ClearEdpCounter(uint8_t edp);
uint8_t USB_ReadEdpData(uint8_t edp);
void    USB_SetEdpCtrlFlag(uint8_t edp, uint8_t USB_Ctrl_Flag);

bool_t   USB_isEdpBusy(uint8_t edp);
void     USB_WriteEdp(uint8_t edp, uint8_t *pData, uint32_t len);
uint32_t USB_ReadEdp(uint8_t edp, uint8_t *pData, uint32_t len);


/******************************************************************************/
/*                                                                            */
/*                      USB Register Bits Define                              */
/*                                                                            */
/******************************************************************************/

/******************************************************************************/
/*  USB Endpoint0 Status Register(EDP0ST) Bits Define                         */
/******************************************************************************/
#define USB_EDP0_STA_TRANS    (uint8_t)0x0F
#define USB_EDP0_STA_SETUP    (uint8_t)0x10 /*!< Setup stage */
#define USB_EDP0_STA_DATA     (uint8_t)0x20 /*!< Setup data stage */
#define USB_EDP0_STA_STATUS   (uint8_t)0x40 /*!< Setup status stage */
#define USB_EDP0_STA_INTF     (uint8_t)0x80

/******************************************************************************/
/*  USB  Endpoint0 Control Register(EDP0CTRL) Bits Define                     */
/******************************************************************************/
#define USB_EDP0_CTRL_DAT_ACK     (uint8_t)0x01
#define USB_EDP0_CTRL_DAT_STALL   (uint8_t)0x02
#define USB_EDP0_CTRL_STA_ACK     (uint8_t)0x04
#define USB_EDP0_CTRL_STA_STALL   (uint8_t)0x08

/******************************************************************************/
/*  USB Endpoint0 Mode Register(EDP0MODE) Bits Define                         */
/******************************************************************************/
#define USB_EDP0_MODE_AUTO_ADDR    (uint8_t)0x01
#define USB_EDP0_MODE_AUTO_CFG     (uint8_t)0x02
#define USB_EDP0_MODE_AUTO_INTF    (uint8_t)0x04
#define USB_EDP0_MODE_AUTO_STA     (uint8_t)0x08
#define USB_EDP0_MODE_AUTO_SYN     (uint8_t)0x10
#define USB_EDP0_MODE_AUTO_DESC    (uint8_t)0x20
#define USB_EDP0_MODE_AUTO_FEAT    (uint8_t)0x40
#define USB_EDP0_MODE_AUTO_STD     (uint8_t)0x80

/******************************************************************************/
/*  USB endpoint(1 - 8) Control Register(EDPSxCTRL) Bits Define               */
/******************************************************************************/
#define USB_EDP_CTRL_BUSY      (uint8_t)0x01
#define USB_EDP_CTRL_STALL     (uint8_t)0x02
#define USB_EDP_CTRL_DAT0      (uint8_t)0x04
#define USB_EDP_CTRL_DAT1      (uint8_t)0x08
#define USB_EDP_CTRL_MONO      (uint8_t)0x40
#define USB_EDP_CTRL_EOF_ISO   (uint8_t)0x80

/******************************************************************************/
/*  USB endpoint(1 - 8) IRQ Mask Register Bits Define                         */
/******************************************************************************/
#define USB_EDP8_IRQ     ((uint8_t)0x01) /*!< endpoint8. */
#define USB_EDP1_IRQ     ((uint8_t)0x02)
#define USB_EDP2_IRQ     ((uint8_t)0x04)
#define USB_EDP3_IRQ     ((uint8_t)0x08)
#define USB_EDP4_IRQ     ((uint8_t)0x10)
#define USB_EDP5_IRQ     ((uint8_t)0x20)
#define USB_EDP6_IRQ     ((uint8_t)0x40)
#define USB_EDP7_IRQ     ((uint8_t)0x80)

#endif /* USB_PORT_H_ */
